//
//  LXChannelView.swift
//  礼物动画展示
//
//  Created by seven on 2017/12/29.
//  Copyright © 2017年 seven. All rights reserved.
//

import UIKit

enum LXGiftChannelStatus{
    case idel
    case animating
    case willEnd
    case endingAnimating
}

class LXChannelView: UIView {
    @IBOutlet weak var bgView: UIView!
    @IBOutlet weak var iconImageView: UIImageView!
    @IBOutlet weak var senderLabel: UILabel!
    @IBOutlet weak var giftDesLabel: UILabel!
    @IBOutlet weak var giftImageView: UIImageView!
    @IBOutlet weak var digitLabel: LXGiftDigitLabel!
    
    private var currentNumber: Int = 0
    private var cacheNumber: Int = 0
    var state: LXGiftChannelStatus = .idel
    
    var complectionCallback : ((LXChannelView) -> ())?
    
    var giftModel: LXGiftModel? {
        didSet{
            guard let giftModel = giftModel else {return}
            
            iconImageView.image = UIImage(named: giftModel.senderURL)
            senderLabel.text = giftModel.senderName
            giftDesLabel.text = "送出礼物：【\(giftModel.giftName)】"
            giftImageView.image = UIImage(named: giftModel.giftURL)
            
            //将chanelView弹出
            state = .animating
            performAnimation()
        }
    }
     

}
//MARK:- 设置UI界面
extension LXChannelView{
    override func layoutSubviews() {
        super.layoutSubviews()
        bgView.layer.cornerRadius = frame.height * 0.5
        iconImageView.layer.cornerRadius = frame.height * 0.5
        bgView.layer.masksToBounds = true
        iconImageView.layer.masksToBounds = true
        iconImageView.layer.borderWidth = 1
        iconImageView.layer.borderColor = UIColor.green.cgColor
    }
}

//MARK:- 对外提供的函数
extension LXChannelView{
    func addOnceToCache(){
        if state == .willEnd {
            performDigitalLabelAnimation()
            NSObject.cancelPreviousPerformRequests(withTarget: self)
        }else{
            cacheNumber += 1
        }
    }
    
    class func loadFromNib() -> LXChannelView {
        return Bundle.main.loadNibNamed("LXChannelView", owner: nil, options: nil)?.first as! LXChannelView
    }
}
//MARK:- 执行动画代码
extension LXChannelView{
    private func performAnimation(){
        digitLabel.alpha = 1.0
        digitLabel.text = " X1 "
        UIView.animate(withDuration: 0.25, animations: {
            self.alpha = 1.0
            self.frame.origin.x = 0
        }) { (isFinished) in
            self.performDigitalLabelAnimation()
        }
    }
    
    private func performDigitalLabelAnimation(){
        currentNumber += 1
        digitLabel.text = " X\(currentNumber) "
        digitLabel.showDigitalAnimation {
            
            if self.cacheNumber > 0{
                self.cacheNumber -= 1
                self.performDigitalLabelAnimation()
            }else {
                self.state = .willEnd
                self.perform(#selector(self.performEndAnimation), with: nil, afterDelay: 3)
            }
            
        }
    }
    
    @objc private func performEndAnimation(){
        state = .endingAnimating
        UIView.animate(withDuration: 0.25, animations: {
            self.frame.origin.x = UIScreen.main.bounds.width
            self.alpha = 0
        }) { (isFinished) in
            self.currentNumber = 0
            self.cacheNumber = 0
            self.giftModel = nil
            self.frame.origin.x = -self.frame.width
            self.state = .idel
            self.digitLabel.alpha = 0.0
            
            if let complectionCallback = self.complectionCallback {
                complectionCallback(self)
            }
        }
    }
}
